package com.cetool.autocms.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cetool.autocms.mapper.sys.MenuMapper;
import com.cetool.autocms.pojo.domain.login.UserInfoDO;
import com.cetool.autocms.pojo.domain.sys.ChildMenuListDO;
import com.cetool.autocms.pojo.dto.sys.MenuListDTO;
import com.cetool.autocms.pojo.param.sys.QueryRoleMenuListParam;
import com.cetool.autocms.service.sys.SysService;
import com.cetool.autocms.util.BeansUtil;
import com.cetool.autocms.util.UserInfoContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统管理服务实现层
 *
 * @author 戴越
 * @date 2021/11/26
 */
@Service
@Slf4j
public class SysServiceImpl implements SysService {
    /**
     * 注入菜单管理持久层
     */
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<MenuListDTO> initMenu() {
        UserInfoDO userData = UserInfoContextUtil.getUserData();
        QueryRoleMenuListParam param = new QueryRoleMenuListParam();
        param.setRoleId(userData.getRoleId());
        List<ChildMenuListDO> roleMenu = menuMapper.getRoleMenu(param);
        if (roleMenu != null && roleMenu.size() > 0){
            Map<Integer, List<ChildMenuListDO>> collect = roleMenu.stream().collect(Collectors.groupingBy(ChildMenuListDO::getPid));
            List<MenuListDTO> menuListS = BeansUtil.listCopy(collect.get(-1), MenuListDTO.class);
            for (MenuListDTO list : menuListS){
                list.setChildMenuList(collect.get(list.getId()));
            }
            return menuListS;
        }else {
            return new ArrayList<MenuListDTO>();
        }
    }
}
